<?php

/**
 * This is the model class for table "{{acledger}}".
 *
 * The followings are the available columns in table '{{acledger}}':
 * @property string $idacledger
 * @property string $acledgername
 * @property integer $idacgroup
 * @property string $openingbalance
 * @property string $currentbalance
 * @property string $openingbalancecrdr
 * @property string $currentbalancecrdr
 *
 * The followings are the available model relations:
 * @property Acgroup $idacgroup0
 * @property Receive[] $receives
 * @property Transactiondetails[] $transactiondetails
 */
class Acledger extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Acledger the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{acledger}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idacgroup, openingbalancecrdr', 'required'),
			array('idacgroup', 'numerical', 'integerOnly'=>true),
			array('acledgername', 'length', 'max'=>40),
			array('openingbalance, currentbalance', 'length', 'max'=>10),
			array('openingbalance, currentbalance', 'numerical', 'min'=>0),
			array('openingbalancecrdr, currentbalancecrdr', 'length', 'max'=>3),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idacledger, acledgername, idacgroup, openingbalance, currentbalance, openingbalancecrdr, currentbalancecrdr', 'safe', 'on'=>'search'),
                        array('acgroupname', 'autocompleteValidator', 'model'=>'Acgroup'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!$params['model']::model()->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['acgroupname'])){
                $match = addcslashes($_REQUEST['acgroupname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'acgroupname = :acgroupname',
                    'params'    => array(':acgroupname' => "$match")
                ) );            
                $model=Acgroup::model()->find($criteria);
                $this->idacgroup = $model->idacgroup;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idacgroup0' => array(self::BELONGS_TO, 'Acgroup', 'idacgroup'),
			'receives' => array(self::HAS_MANY, 'Receive', 'receivedfrom'),
			'transactiondetails' => array(self::HAS_MANY, 'Transactiondetails', 'idacledger'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idacledger' => Yii::t('application', 'Idacledger'),
			'acledgername' => Yii::t('application', 'Acledgername'),
			'idacgroup' => Yii::t('application', 'Idacgroup'),
			'openingbalance' => Yii::t('application', 'Openingbalance'),
			'currentbalance' => Yii::t('application', 'Currentbalance'),
			'openingbalancecrdr' => Yii::t('application', 'Openingbalancecrdr'),
			'currentbalancecrdr' => Yii::t('application', 'Currentbalancecrdr'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idacledger',$this->idacledger,true);
		$criteria->compare('acledgername',$this->acledgername,true);
		$criteria->compare('idacgroup',$this->idacgroup);
		$criteria->compare('openingbalance',$this->openingbalance,true);
		$criteria->compare('currentbalance',$this->currentbalance,true);
		$criteria->compare('openingbalancecrdr',$this->openingbalancecrdr,true);
		$criteria->compare('currentbalancecrdr',$this->currentbalancecrdr,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}